package com.example.dollarShipping.controller;


import com.example.dollarShipping.entity.ShippingOrder;
import com.example.dollarShipping.service.AccountService;
import com.example.dollarShipping.utils.Result;
import com.example.dollarShipping.utils.ResultCode;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController()
@RequestMapping("/account")
public class AccountController {

    @Resource
    AccountService accountService;

    ResultCode resultCode = new ResultCode();


    @GetMapping("/shippingOrderList")
    @PreAuthorize("hasAnyAuthority('admin','carrier','account')")
    public Result getShippingOrderList(@RequestParam(required = false,name = "id") Long id){
        Result res = new Result();
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            List<ShippingOrder> list = accountService.getShippingOrder(id);
            map.put("shippingOrderList",list);
            res.setMessage("货运单返回成功");
            res.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(resultCode.InfoGetERR);
            res.setMessage(resultCode.getMsg(resultCode.InfoGetERR));
        }
        return res;
    }

    //创建货运单
    @PostMapping("createShippingOrder")
    @PreAuthorize("hasAnyAuthority('admin','account')")
    public Result createShippingOrder(@RequestBody ShippingOrder s){
        Result res = new Result();
        try {
            accountService.createShippingOrder(s);
            res.setMessage("创建货运单成功");
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(ResultCode.InfoAddERR);
            res.setMessage(resultCode.getMsg(ResultCode.InfoAddERR));
        }

        return res;
    }

    //查看货运单详情
    @PostMapping("shippingOrderDetail")
    @PreAuthorize("hasAnyAuthority('admin','account')")
    public Result shippingOrderDetail(@RequestParam(required = false,name = "id") Long id){
        Result res = new Result();
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            map.put("shippingOrder",accountService.findShippingOrder(id));
            res.setMessage("返回货运单详情");
            res.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(ResultCode.InfoGetERR);
            res.setMessage(resultCode.getMsg(ResultCode.InfoGetERR));
        }

        return res;
    }

    // 删除货运单
    @DeleteMapping("/delShippingOrder")
    @PreAuthorize("hasAnyAuthority('admin','account')")
    public Result delShippingOrder(@RequestParam Long id){
        Result res = new Result();
        try {
            accountService.delete(id);
            res.setMessage("货运单删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(ResultCode.InfoDelERR);
            res.setMessage(resultCode.getMsg(ResultCode.InfoDelERR));
        }
        return res;
    }

    // 货运单签收/结算操作
    // 修改货运单状态并添加任务记录
    @PostMapping("/accountOperate")
    @PreAuthorize("hasAnyAuthority('admin','account')")
    public Result accountOperate(@Validated @RequestBody  Map<String,Object> params){
        Integer state = (Integer) params.get("state");//8-签收，9-结算
        Long id = Long.parseLong(params.get("orderId").toString());
        Long operator = Long.parseLong(params.get("operator").toString());
        Result res = new Result();
        try {
            String r = accountService.changeState(state, id, operator);
            res.setMessage(r);
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(ResultCode.OpERR);
            res.setMessage(resultCode.getMsg(ResultCode.OpERR));
        }
        return res;
    }

}
